package cn.jxau.fukin.service.impl;

import static cn.jxau.fukin.util.ConstantUtil.SUCCESS;
import static cn.jxau.fukin.util.PKUtil.getPrimaryKey;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jxau.fukin.dao.NotifyMapper;
import cn.jxau.fukin.dao.UserMapper;
import cn.jxau.fukin.model.Notify;
import cn.jxau.fukin.model.NotifyExample;
import cn.jxau.fukin.model.NotifyExample.Criteria;
import cn.jxau.fukin.model.User;
import cn.jxau.fukin.model.UserExample;
import cn.jxau.fukin.service.INotifyService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service("notifyService")
public class NotifyService implements INotifyService {

	@Autowired
	NotifyMapper notifyMapper;
	@Autowired
	UserMapper userMapper;

	// 将发送的通知存储到数据库
	public void saveNotify(User user, String title, String content) {
		Notify notify = new Notify();
		notify.setNotifyId(getPrimaryKey());
		notify.setNotifyTitle(title);
		notify.setNotifyContent(content);
		notify.setuId(user.getuId());
		notifyMapper.insert(notify);
	}

	// 删除通知
	public String deleteNotify(String notifyId) {
		notifyMapper.deleteByPrimaryKey(notifyId);
		return SUCCESS;
	}

	// 将消息设为已读
	public String setNotifyRead(String notifyId) {
		Notify notify = notifyMapper.selectByPrimaryKey(notifyId);
		notify.setIsread(true);
		notifyMapper.updateByPrimaryKey(notify);
		return SUCCESS;
	}

	// 按条件查询
	public List<Notify> selectNotify(Notify notify) {
		if (notify == null)
			return null;
		NotifyExample notifyExample = new NotifyExample();
		Criteria criteria = notifyExample.createCriteria();
		judge(notify, criteria);
		return notifyMapper.selectByExample(notifyExample);
	}

	void judge(Notify notify, Criteria criteria) {
		if (notify.getIsread() != null) {
			criteria.andIsreadEqualTo(notify.getIsread());
		}
		if (notify.getNotifyContent() != null && !"".equals(notify.getNotifyContent().trim())) {
			criteria.andNotifyContentEqualTo(notify.getNotifyContent());
		}
		if (notify.getNotifyId() != null && !"".equals(notify.getNotifyId().trim())) {
			criteria.andNotifyIdEqualTo(notify.getNotifyId());
		}
		if (notify.getNotifyTitle() != null && !"".equals(notify.getNotifyTitle().trim())) {
			criteria.andNotifyIdEqualTo(notify.getNotifyTitle());
		}
		if (notify.getNotifyTime() != null) {
			criteria.andNotifyTimeEqualTo(notify.getNotifyTime());
		}
	}

	@Override
	public PageInfo<Notify> selectNotifyByUser(User user, int page, int rows) {
		UserExample userExample = new UserExample();
		userExample.createCriteria().andUNameEqualTo(user.getuName());
		List<User> userList = userMapper.selectByExample(userExample);

		NotifyExample notifyExample = new NotifyExample();
		notifyExample.createCriteria().andUIdEqualTo(userList.get(0).getuId());
		PageHelper.startPage(page, rows);
		List<Notify> notifyList = notifyMapper.selectByExample(notifyExample);

		PageInfo<Notify> pageInfo = new PageInfo<Notify>(notifyList);
		return pageInfo;
	}

	@Override
	public void updateNotifyInfo(Notify notify) {

		notifyMapper.updateByPrimaryKeySelective(notify);
	}

}
